package com.naiterui.ehp.bs.emr.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.naiterui.ehp.bp.utils.date.DateUtil;
import com.naiterui.ehp.bp.utils.lang.StringUtil;
import com.naiterui.ehp.bs.emr.entity.mongo.MedicalRecord;
import com.naiterui.ehp.bs.emr.entity.mongo.MedicalRecordDoctor;
import com.naiterui.ehp.bs.emr.entity.mongo.PatientDisease;
import com.naiterui.ehp.bs.emr.repository.mongo.MedicalRecordRepository;
import com.naiterui.ehp.bs.emr.repository.mongo.PatientDiseaseRepository;
import com.naiterui.ehp.bs.emr.service.ISuperviseService;
import com.naiterui.ehp.bs.emr.vo.SuperviseDiagnosisVO;
import com.naiterui.ehp.bs.emr.vo.SuperviseFirstToDoctorVO;
import com.naiterui.ehp.bs.emr.vo.SuperviseMedicalRecordVO;

/**
 * <pre>
 *
 * </pre>
 *
 * @author fangguanhong
 * @date 2021/1/15 上午11:43
 * @since 1.0.0
 */
@Service
public class SuperviseServiceImpl implements ISuperviseService {

    @Autowired
    private MedicalRecordRepository medicalRecordRepository;
    @Autowired
    private PatientDiseaseRepository patientDiseaseRepository;

    @Override
    public List<SuperviseMedicalRecordVO> medicalRecord(String sessionId, Long patientId, Long doctorId) {
        List<SuperviseMedicalRecordVO> result = new ArrayList<>();
        List<MedicalRecord> medicalRecords = medicalRecordRepository.findBySessionId(sessionId);
        // 病情描述--包含初诊医疗机构信息
        PatientDisease patientDisease = patientDiseaseRepository.findFirstByPatientIdOrderByCreateAtDesc(patientId);
        if (CollectionUtil.isNotEmpty(medicalRecords)) {
            medicalRecords.forEach(record -> {
                MedicalRecordDoctor recordDoctor = record.getRecordDoctor();
                if (null != recordDoctor) {
                    List<String> diagnosisList = recordDoctor.getDiagnosisList();
                    SuperviseMedicalRecordVO medicalRecord =
                            SuperviseMedicalRecordVO.builder()
                                                    .id(record.getId())
                                                    .allergyTag(0)
                                                    .allergy(null)
                                                    .serviceTime(DateUtil.formatDate(record.getCreateAt(), DateUtil.DATE_TIME_FORMAT))
                                                    .caseMainComplaint(recordDoctor.getMainComplaint())
                                                    .casePresentDisease(recordDoctor.getPresentDisease())
                                                    .casePast(recordDoctor.getPastHistory())
                                                    .firstHospitalCode("HD111")
                                                    .firstHospitalName(null != patientDisease ?
                                                            patientDisease.getOfflineHospital() : "")
                                                    .firstDescription(patientDisease.getDescription())
                                                    .createdAt(DateUtil.formatDate(record.getCreateAt(), DateUtil.DATE_TIME_FORMAT))
                                                    //.diagnosisName(CollectionUtil.isNotEmpty(diagnosisList) ? diagnosisList.get(0) : null)
                                                    //.diagnosisCode(null)
                                                    .build();
                    if (CollectionUtil.isNotEmpty(diagnosisList)) {
                        medicalRecord.setDiagnosis(diagnosisList.stream()
                                                                .map(str -> SuperviseDiagnosisVO.builder().name(str).code(null).build())
                                                                .collect(Collectors.toList()));
                    }
                    result.add(medicalRecord);
                }
            });
        }
        return result;
    }

    @Override
    public SuperviseFirstToDoctorVO firstToDoctor(Long patientId) {
        // 患者发起咨询时填写的 病情描述
        PatientDisease patientDisease = patientDiseaseRepository.findFirstByPatientIdOrderByCreateAtDesc(patientId);
        if (null != patientDisease) {
            return SuperviseFirstToDoctorVO.builder()
                                           .firstHospitalCode("111111")
                                           .firstHospitalName(patientDisease.getOfflineHospital())
                                           .firstDescription(patientDisease.getDescription())
                                           .build();
        }
        return new SuperviseFirstToDoctorVO("", "", "");
    }
}
